package com.topteam.demo.service

import org.apache.deltaspike.core.api.jmx.MBean
import javax.inject.Inject
import javax.persistence.EntityManager
import com.topteam.demo.domain.{User, Todo}
import org.apache.deltaspike.jpa.api.transaction.Transactional
import javax.enterprise.inject.Default
import com.topteam.core.Second

/**
 * Created by thinkpad on 14-1-24.
 */
@MBean
class TodoService extends Serializable {

  @Inject
  @Default private var em: EntityManager = _
  @Inject
  @Second private var em2: EntityManager = _

  @Transactional(qualifier = Array(classOf[Default]))
  def addNewTodo(todo: Todo): Todo = {
    em.persist(todo)
    todo
  }

  @Transactional(qualifier = Array(classOf[Default], classOf[Second]))
  def addNewTodo(todo: Todo, user: User): Todo = {
    em.persist(todo)
    em2.persist(user)
    todo
  }
}
